<?php
namespace app\widgets;

use yii\helpers\Html;
use yii\helpers\Url;

class LinkPager extends \yii\widgets\LinkPager {
	/**
	 *
	 * @var array HTML attributes for the go num input tag.
	 */
	public $inputOptions = [];
	/**
	 *
	 * @var array HTML attributes for the go button tag.
	 */
	public $goOptions = [];
	/**
	 *
	 * @var boolean Show statistics data.
	 */
	public $showStatistics = true;
	/**
	 *
	 * @var boolean Show goto pagenum button.
	 */
	public $showGoBtn = true;

	/**
	 * Executes the widget.
	 * This overrides the parent implementation by displaying the generated page buttons.
	 */
	public function run() {
		if ($this->registerLinkTags) {
			$this->registerLinkTags();
		}
		
		echo $this->renderPageButtons();
	}

	/**
	 * Renders the page buttons.
	 *
	 * @return string the rendering result
	 */
	protected function renderPageButtons() {
		$pageCount = $this->pagination->getPageCount();
		if ($pageCount < 2 && $this->hideOnSinglePage) {
			return '';
		}
		
		$buttons = [];
		$currentPage = $this->pagination->getPage();
		
		// first page
		$firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
		if ($firstPageLabel !== false) {
			$buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
		}
		
		// prev page
		if ($this->prevPageLabel !== false) {
			if (($page = $currentPage - 1) < 0) {
				$page = 0;
			}
			$buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
		}
		
		// internal pages
		list ($beginPage, $endPage) = $this->getPageRange();
		for ($i = $beginPage; $i <= $endPage; ++$i) {
			$buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
		}
		
		// next page
		if ($this->nextPageLabel !== false) {
			if (($page = $currentPage + 1) >= $pageCount - 1) {
				$page = $pageCount - 1;
			}
			$buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
		}
		
		// last page
		$lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
		if ($lastPageLabel !== false) {
			$buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
		}
		
		// Go button
		if ($this->showGoBtn) {
			$inputOptions = array_merge($this->inputOptions, [
				'style'=>'height:33px; width:35px; float:left;'
			]);
			$buttons[] = Html::tag('li', Html::textInput('page_num', $this->pagination->page + 1, $inputOptions), $this->options);
			$goOptions = array_merge($this->goOptions, [
				'style'=>'height:33px; float:left;',
				'onclick'=>'window.location=\'' . Url::current([
					'page'=>'A761208'
				]) . '\'.replace(\'A761208\', $(\'input[name=page_num]\').val())'
			]);
			$buttons[] = Html::tag('li', Html::button('Go', $goOptions), $this->options);
		}
		
		if ($this->showStatistics) {
			$buttons[] = Html::tag('li', Html::label('总数：' . $this->pagination->totalCount . '；总页数：' . $this->pagination->pageCount . '；', '', [
				'style'=>'line-height:33px;'
			]), $this->options);
		}
		
		return Html::tag('ul', implode("\n", $buttons), $this->options);
	}
}
